 

libname pop "C:\Data\Populations States 2000-2009";

%macro pop_c(year,state1,state2,state3,state4,state5);
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk1 sum = all&year.;
   where NAME in ("&state1.","&state2.","&state3.","&state4.","&state5.") and age in (22,23) and sex = 0;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk2 sum = male&year.;
   where NAME in ("&state1.","&state2.","&state3.","&state4.","&state5.") and age in (22,23) and sex = 1;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk3 sum = female&year.;
   where NAME in ("&state1.","&state2.","&state3.","&state4.","&state5.") and age in (22,23) and sex = 2;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk4 sum = all_AZ&year.;
   where NAME = "Arizona" and age in (22,23) and sex = 0;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk5 sum = all_CA&year.;
   where NAME = "California" and age in (22,23) and sex = 0;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk6 sum = all_IA&year.;
   where NAME = "Iowa" and age in (22,23) and sex = 0;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk7 sum = all_NJ&year.;
   where NAME = "New Jersey" and age in (22,23) and sex = 0;
run;
proc means data = pop.Sc_est2009_agesex_res noprint;
   var POPESTIMATE&year.;
   output out = junk8 sum = all_WI&year.;
   where NAME = "Wisconsin" and age in (22,23) and sex = 0;
run;
data pop&year. (drop = _type_ _freq_);
   merge junk1 junk2 junk3 junk4 junk5 junk6 junk7 junk8;
run;
proc datasets;
   delete junk1 junk2 junk3 junk4 junk5 junk6 junk7 junk8;
run;
%mend;

%pop_c(2004,XXXXXXX,XXXXXXXXXX,Iowa,New Jersey,Wisconsin);
%pop_c(2005,Arizona,California,Iowa,New Jersey,Wisconsin);
%pop_c(2006,Arizona,California,Iowa,New Jersey,Wisconsin);
%pop_c(2007,Arizona,California,Iowa,New Jersey,Wisconsin);
%pop_c(2008,Arizona,XXXXXXXXXX,Iowa,New Jersey,Wisconsin);
%pop_c(2009,Arizona,XXXXXXXXXX,Iowa,New Jersey,Wisconsin);


data inter.ins_23_pop_combined (keep = merge_v pop_all_AZ_CA_IA_NJ_WI pop_female_AZ_CA_IA_NJ_WI pop_male_AZ_CA_IA_NJ_WI pop_AZ pop_CA pop_IA pop_NJ pop_WI);
   merge pop2004 pop2005 pop2006 pop2007 pop2008 pop2009;
   pop_all_AZ_CA_IA_NJ_WI = (all2004 + all2005 + all2006 + all2007 + all2008 + all2009)/(2*12);
   pop_female_AZ_CA_IA_NJ_WI = (female2004 + female2005 + female2006 + female2007 + female2008 + female2009)/(2*12);
   pop_male_AZ_CA_IA_NJ_WI = (male2004 + male2005 + male2006 + male2007 + male2008 + male2009)/(2*12);
   pop_AZ = (             all_AZ2005 + all_AZ2006 + all_AZ2007 + all_AZ2008 + all_AZ2009)/(2*12);
   pop_CA = (             all_CA2005 + all_CA2006 + all_CA2007                          )/(2*12);
   pop_IA = (all_IA2004 + all_IA2005 + all_IA2006 + all_IA2007 + all_IA2008 + all_IA2009)/(2*12);
   pop_NJ = (all_NJ2004 + all_NJ2005 + all_NJ2006 + all_NJ2007 + all_NJ2008 + all_NJ2009)/(2*12);
   pop_WI = (all_WI2004 + all_WI2005 + all_WI2006 + all_WI2007 + all_WI2008 + all_WI2009)/(2*12);
   merge_v = 1;
run;

data test;
   set pop.Sc_est2009_agesex_res;
   sum_pop = POPESTIMATE2004 + POPESTIMATE2005 + POPESTIMATE2006 + POPESTIMATE2007 + POPESTIMATE2008 + POPESTIMATE2009;
run;

proc means data = test sum;
   var sum_pop;
   where NAME in ("Arizona","California","Iowa","New Jersey","Wisconsin") and age = 22 and sex = 0;
   title "Age 22";
run;

proc means data = test sum;
   var sum_pop;
   where NAME in ("Arizona","California","Iowa","New Jersey","Wisconsin") and age = 23 and sex = 0;
   title "Age 23";
run;

proc datasets;
   delete test pop2004 pop2005 pop2006 pop2007 pop2008 pop2009;
run;
